import { StyleObj, food } from './UseFood'
import { SnakeStyle, addBody, isLive, Snakebody } from './UseSnakeMovement'
import { addFraction, Fraction } from './UseFraction'
import { HelpData, elData, SpeedPercentage, GetDifficulty } from './UseHelp'
import { ref } from 'vue';
// 设置一个变量限制等级
let maxGrade: number;
// 设置一个变量表示多少分时升级
let upfraction: number;
// 创建一个属性来存储蛇的移动方向（按键的方向）
let direction: string = '';
//储存上一次蛇的方向
let a: string = '';
//设置蛇的速度和困难
let Speed = ref(0);

// 游戏的初始化方法，调用后游戏即开始
function init() {
  document.addEventListener('keyup', keydownHandler.bind(event))
  SnakeMove()

}
// 创建一个键盘按下的响应函数
function keydownHandler(event: KeyboardEvent) {
  console.log('event.code:',event.code);
  //判读用户是否看完规则
  if (HelpData.height == 400) return
  // 需要检查event.key的值是否合法（用户是否按了正确的按键）
  if (event.key == event.code || event.code == 'Space') {
    // 修改direction属性
    direction = event.code;
    //当身体不为一时，判断蛇是否掉头
    if (Snakebody.length >= 2) {
      direction = SnakeTurnAround(direction, a)
    }
    direction == 'Space' ? 1 : a = direction
  }

}
//点击蛇移动
function ClickMove(Click: string) {
  direction = Click
  //当身体不为一时，判断蛇是否掉头
  if (Snakebody.length >= 2) {
    direction = SnakeTurnAround(direction, a)
  }
  direction == '' ? 1 : a = direction
}
//蛇移动
function SnakeMove() {
  switch (direction) {
    case "ArrowUp":
    case "Up":
      // 向上移动 top 减少
      SnakeStyle.top -= 10;
      break;
    case "ArrowDown":
    case "Down":
      // 向下移动 top 增加
      SnakeStyle.top += 10;
      break;
    case "ArrowLeft":
    case "Left":
      // 向左移动 left 减少
      SnakeStyle.left -= 10;
      break;
    case "ArrowRight":
    case "Right":
      // 向右移动 left 增加
      SnakeStyle.left += 10;
      break;
  }
  // 检查蛇吃到了食物
  checkEat()
  //储存蛇的速度，等级和用户设置的速度和困难
  Speed.value = (300 - (Fraction.Grade - 1) * 30) - (elData.value1 / 10 + 5) - (elData.value2 / 10 + 5)
  Speed.value <= 0 ? Speed.value = 10 : Speed
  //获取当前蛇的速度
  SpeedPercentage()
  //获取当前困难等级
  HelpData.Difficulty = Fraction.fraction + (Math.round(elData.value1 / 10)) + (Math.round(elData.value2 / 10))
  GetDifficulty()
  //开启一个定时器，让蛇一直跑
  isLive.value && setTimeout(SnakeMove.bind(direction), Speed.value)
}

//蛇是否吃到食物
function checkEat() {
  if (StyleObj.left == SnakeStyle.left && StyleObj.top == SnakeStyle.top) {
    // 食物的位置进行重置
    food();
    //  分数增加
    addFraction(upfraction = 2, maxGrade = 10)
    //增加一节身体
    addBody()

  }
}
// 检查蛇是否原地掉头
function SnakeTurnAround(direction: string, a: string): string {
  if (direction == 'ArrowLeft' || direction == 'Left') {
    if (a == 'ArrowRight' || a == 'Right') {
      direction = 'ArrowRight'
    }
  }
  if (direction == 'ArrowRight' || direction == 'Right') {
    if (a == 'ArrowLeft' || a == 'Left') {
      direction = 'ArrowLeft'
    }
  }
  if (direction == 'ArrowUp' || direction == 'Up') {
    if (a == 'ArrowDown' || a == 'Down') {
      direction = 'ArrowDown'
    }
  }
  if (direction == 'ArrowDown' || direction == 'Down') {
    if (a == 'ArrowUp' || a == 'Up') {
      direction = 'ArrowUp'
    }
  }
  return direction
}

// init()
export {
  Speed,
  init,
  ClickMove,
}
